package com.yixun.qfbao.hander;

import com.yixun.qfbao.api.service.WxmsgFailureApiService;
import com.yixun.qfbao.apidto.SendFailureWxmsgAgainReqDto;
import com.yixun.qfbao.apidto.SendFailureWxmsgAgainResDto;
import com.yixun.qfbao.dto.base.BaseRequestDTO;
import com.yixun.qfbao.dto.base.BaseResponseDTO;
import com.yixun.qfbao.hander.base.CommonAbstractHander;
import com.yixun.qfbao.model.WxmsgFailure;
import com.yixun.qfbao.model.dto.WxmsgFailureDto;
import com.yixun.qfbao.model.enums.SystemIdEnum;
import com.yixun.qfbao.service.WxmsgFailureService;
import com.yixun.qfbao.util.TemplateMessageResult;
import com.yixun.qfbao.util.WxAccessTokenCacheUtils;
import com.yixun.qfbao.vo.SendFailureWxmsgAgainReqVo;
import com.yixun.qfbao.xf.model.enums.ResultEnum;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class SendFailureWxmsgAgainHander extends CommonAbstractHander {

    private static Logger logger= LoggerFactory.getLogger(SendFailureWxmsgAgainHander.class);

    @Autowired
    private WxmsgFailureApiService wxmsgFailureApiService;
    @Autowired
    private WxAccessTokenCacheUtils wxAccessTokenCacheUtils;
    @Autowired
    private WxmsgFailureService wxmsgFailureService;

    @Override
    public BaseResponseDTO doBusiness(BaseRequestDTO reqDto) {

        SendFailureWxmsgAgainReqVo req = (SendFailureWxmsgAgainReqVo)reqDto;
        SendFailureWxmsgAgainReqDto reqDto1 = req.getParams();
        BaseResponseDTO<SendFailureWxmsgAgainResDto> res = new BaseResponseDTO<>();
        SendFailureWxmsgAgainResDto resDto = new SendFailureWxmsgAgainResDto();
        try{
            TemplateMessageResult result = new TemplateMessageResult();
            WxmsgFailureDto wfDto = new WxmsgFailureDto();
            wfDto.setId(reqDto1.getId());
            if(null != reqDto1.getId()){
                WxmsgFailure wxmsgFailure = wxmsgFailureService.selectById(reqDto1.getId());
                if(null != wxmsgFailure){
                    String accessToken = wxAccessTokenCacheUtils.getAccessTokenBySystemId(wxmsgFailure.getCompanyUuid(), wxmsgFailure.getSystemId(), null);
                    result = wxmsgFailureApiService.sendFailureWxmsgAgain(wxmsgFailure, accessToken);
                }
            }
            resDto.setData(result);
            res.setResData(resDto);
            res.setResult(resDto.getResult());
        }catch (Exception e){
            logger.error("SendFailureWxmsgAgainHander error: {}", e);
            res.setResult(ResultEnum.FAIL.getNo());
            res.setErrorMsg("设置短信模板开关失败");
        }
        return res;
    }
}
